package com.ck.ck_back.controller;

import com.ck.ck_back.pojo.Content;
import com.ck.ck_back.pojo.Result;
import com.ck.ck_back.service.contentservice;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/content")
public class ContentController {
    @Resource
    private contentservice contentserviceimpl;

//    插入文章内容，并返回内容的id
////    404000：参数错误，300000无此人,200000此人身份错误，不然就是id
    @PostMapping("/insert")
    public Object insert_content(String user_id, String content){
        int result = contentserviceimpl.insert_Content(user_id,content);
        if (result == 404000){
            return Result.failure("参数错误");
        }else if (result == 300000){
            return Result.failure("查无此人");
        }else if (result == 200000){
            return Result.failure("身份错误");
        }else return Result.success(result);
    }

//    根据文章id拿文章
    @PostMapping("/select")
    public Object select(int content_id){
        Content content = contentserviceimpl.selectbyid(content_id);
        if (content !=null){
            return Result.success(content);
        }else return Result.failure("内容为空");
    }

    //    //    根据id更新内容,404参数错误,3无此文章，先创建，2更新失败，1成功
    @PostMapping("/update")
    public Object update(int content_id,String content){
        int result = contentserviceimpl.update(content_id,content);
        if (result == 1){
            return Result.success("更新成功");
        }else if (result == 2){
            return  Result.failure("更新失败");
        }else if (result == 3){
            return  Result.failure("无此文章，请先创建");
        }else {
            return  Result.failure("参数错误");
        }
    }
}
